package com.zs.hydrant;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;

public class md5Util {
    // 将明文转换为MD5哈希字符串
    public static String md5Encode(String plainPassword) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] byteArray = plainPassword.getBytes(StandardCharsets.UTF_8);
            byte[] md5Bytes = md.digest(byteArray);

            // 把字节数组转成16进制字符串
            StringBuilder hexStr = new StringBuilder();
            for (byte b : md5Bytes) {
                int val = ((int) b) & 0xff;
                if (val < 16) {
                    hexStr.append("0");
                }
                hexStr.append(Integer.toHexString(val));
            }
            return hexStr.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}